package dxn.nl.file.api;

import dxn.boot.common.entity.AuthUser;
import dxn.boot.common.entity.BaseResult;
import dxn.boot.common.entity.CsBasePageQuery;
import dxn.boot.common.entity.CsBasePageResult;
import dxn.boot.starter.web.control.BaseControl;
import dxn.boot.starter.web.security.AuthUtils;
import dxn.nl.file.client.model.enums.ExportFileTypeEnum;
import dxn.nl.file.service.CsExportAppModelService;
import dxn.nl.file.service.CsExportService;
import dxn.nl.file.biz.po.AppModelPo;
import dxn.nl.file.biz.po.FileExportApplyDetail;
import dxn.nl.file.biz.vo.CsApplyExportVo;
import dxn.nl.file.biz.vo.CsExportVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/cs/export")
@Tag(name = "Excel导出管理")
public class CsExportControl extends BaseControl {

    @Resource
    private CsExportService csExportService;

    @Resource
    private CsExportAppModelService csExportAppModelService;

    @Operation(summary = "分页查询导出申请")
    @RequestMapping(value = "/pageVo", method = RequestMethod.POST)
    public BaseResult<CsBasePageResult<FileExportApplyDetail>> pageVo(@RequestBody @Valid CsBasePageQuery<CsExportVo> pageQuery) {
        if(pageQuery.getQuery() == null)
        {
            return BaseResult.fail("请求内容不能为空");
        }

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(StringUtils.isBlank(userDetails.getAreaCode()) || !userDetails.getOrgCode().equals(userDetails.getAreaCode()))
        {
            pageQuery.getQuery().setApplyUserCode(userDetails.getCode());
        }

        CsBasePageResult<FileExportApplyDetail> result = csExportService.pageVo(pageQuery);

        return BaseResult.success(result);
    }

    @Operation(summary = "申请导出")
    @RequestMapping(value = "/applyExport", method = RequestMethod.POST)
    public BaseResult<String> applyExport(@RequestBody @Valid CsApplyExportVo vo) {

        AuthUser userDetails = AuthUtils.getCurrentUser();

        AppModelPo modelPo = csExportAppModelService.selectByMode(vo.getModeCode(), ExportFileTypeEnum.export.code);
        if(modelPo == null)
        {
            return BaseResult.fail("当前模块没有配置导出");
        }

        if(modelPo.getAuthTypeCode() == 1)
        {
            // 判断网点权限
        }
        else if (modelPo.getAuthTypeCode() ==2)
        {
            // 判断用户权限
        }

        FileExportApplyDetail applyDetail = new FileExportApplyDetail();

        BeanUtils.copyProperties(vo,applyDetail);

        applyDetail.setApplyTime(LocalDateTime.now());
        applyDetail.setApplyUserCode(userDetails.getCode());
        applyDetail.setApplyUserName(userDetails.getName());
        applyDetail.setOrgCode(userDetails.getOrgCode());
        applyDetail.setOrgName(userDetails.getOrgName());
        applyDetail.setStatus(0);
        applyDetail.setAppId(modelPo.getAppId());
        applyDetail.setModeName(modelPo.getModeName());
        applyDetail.setTag(modelPo.getTag());

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);


        applyDetail.setFileName(applyDetail.getModeName()+"-"+localTime+".xlsx");

        String result = csExportService.applyExport(applyDetail);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "重新处理")
    @RequestMapping(value = "/reDeal", method = RequestMethod.POST)
    public BaseResult<String> updateCanConsume(@RequestBody @Valid FileExportApplyDetail applyDetail) {

        AuthUser userDetails = AuthUtils.getCurrentUser();

        if(!"999999".equals(applyDetail.getOrgCode())
        && !userDetails.getOrgCode().equals(applyDetail.getOrgCode()))
        {
            return BaseResult.fail("非本网点数据");
        }

        String result = csExportService.updateCanConsume(applyDetail);

        return checkAndReturnResult(result);
    }
}